package huxibianjie.com.lbscandroid.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import huxibianjie.com.lbscandroid.R;
import huxibianjie.com.lbscandroid.event.ClickEvent;
import huxibianjie.com.lbscandroid.event.LoginEvent;
import huxibianjie.com.lbscandroid.util.AppManager;
import huxibianjie.com.lbscandroid.util.StatusBarUtil;

/**
 * BaseActivity
 * Created by CZF on 2018/7/23.
 */
public abstract class BaseActivity extends AppCompatActivity {
    private Toast toast;
    private Dialog mDialog;
    private ImageView ivLoading;
    private TextView tvLoading;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else {
            StatusBarUtil.setColor(this, 0x000000);
        }
        EventBus.getDefault().register(this);

        int layoutId = getContentView();
        if (layoutId != 0)
            setContentView(layoutId);
        AppManager.getAppManager().addActivity(this);
        ButterKnife.bind(this);
        initView();
        initListener();
        initData();
    }

    @LayoutRes
    protected abstract int getContentView();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void initListener();

    @SuppressLint("ShowToast")
    public void showToast(int resId) {
        try {
            cancelToast();
            if (toast == null) {
                toast = Toast.makeText(this, resId, Toast.LENGTH_SHORT);
            }
            toast.show();
        } catch (Exception e) {

        }

    }

    @SuppressLint("ShowToast")
    public void showToast(String msg) {
        try {
            cancelToast();
            if (toast == null) {
                toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
            }
            toast.show();
        } catch (Exception e) {

        }
    }

    public void cancelToast() {
        if (toast != null) {
            toast.cancel();
            toast = null;
        }
    }

    public void cancelLoading() {
        if (ivLoading != null) {
            ivLoading.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                }
            }, 500);
        }
    }

    /**
     * @param activity
     * @param id
     * @return T
     * @directions 获取View
     */
    public <T extends View> T getView(Activity activity, int id) {
        View rootView = activity.getWindow().getDecorView().findViewById(android.R.id.content);
        View childView = rootView.findViewById(id);
        return (T) childView;
    }

    public void showLoading() {
        showLoading(getString(R.string.loading));
    }

    protected void showLoading(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mDialog == null || tvLoading == null || ivLoading == null) {
                    View view = LayoutInflater.from(BaseActivity.this).inflate(R.layout.layout_loading, null, false);
                    tvLoading = (TextView) view.findViewById(R.id.tvContent);
                    ivLoading = (ImageView) view.findViewById(R.id.ivLoading);
                    mDialog = new Dialog(BaseActivity.this, R.style.dialog_transparent);
                    mDialog.setCancelable(false);
                    mDialog.setContentView(view);
                    Window win = mDialog.getWindow();
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
                    params.setMargins(0, 48, 0, 0);
                    if (win.getDecorView() != null)
                        win.getDecorView().setLayoutParams(params);
                    mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            if ((ivLoading.getDrawable()) != null)
                                ((AnimationDrawable) ivLoading.getDrawable()).stop();
                        }
                    });
                    mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                        @Override
                        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                            if (keyCode == KeyEvent.KEYCODE_BACK) {
                                dialog.dismiss();
                                finish();
                            }
                            return false;
                        }
                    });
                }
                if (msg == null) {
                    tvLoading.setVisibility(View.GONE);
                } else {
                    tvLoading.setVisibility(View.VISIBLE);
                    tvLoading.setText(msg);
                }
//                Glide.with(BaseActivity.this).load(R.drawable.ic_loading).asGif()
//                        .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(ivLoading);
                if (!mDialog.isShowing()) {
                    if ((ivLoading.getDrawable()) != null)
                        ((AnimationDrawable) ivLoading.getDrawable()).start();
                    mDialog.show();
                }
            }
        });
    }


    protected final void initTitle(String strContent) {
        this.initTitle(0, null, strContent, null, 0);
    }

    protected final void initTitle(int resLeft, String strContent) {
        this.initTitle(resLeft, null, strContent, null, 0);
    }

    protected final void initTitle(int resLeft, String strContent, String strRight) {
        this.initTitle(resLeft, null, strContent, strRight, 0);
    }

    protected final void initTitle(int resLeft, String strContent, int resRight) {
        this.initTitle(resLeft, null, strContent, null, resRight);
    }

    protected final void initTitle(String strLeft, String strContent, String strRight) {
        this.initTitle(0, strLeft, strContent, strRight, 0);
    }

    protected final void initTitle(int resLeft, String strLeft, String strContent, String strRight, int resRight) {
        LinearLayout layTitle = findViewById(R.id.lay_title);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            layTitle.setPadding(0, (int) getResources().getDimension(R.dimen.x80), 0, 0);
        } else {
            layTitle.setPadding(0, 0, 0, 0);
        }
        ImageView ivTitleLeft = (ImageView) findViewById(R.id.iv_title_left);
        ImageView ivTitleRight = (ImageView) findViewById(R.id.iv_title_right);
        TextView tvTitleLeft = (TextView) findViewById(R.id.tv_title_left);
        TextView tvTitleContent = (TextView) findViewById(R.id.tv_title_content);
        TextView tvTitleRight = (TextView) findViewById(R.id.tv_title_right);
        if (ivTitleLeft != null) {
            if (resLeft == 0) {
                ivTitleLeft.setVisibility(View.GONE);
            } else {
                ivTitleLeft.setVisibility(View.VISIBLE);
                ivTitleLeft.setImageResource(resLeft);
                ivTitleLeft.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onClickTitleLeft(v);
                    }
                });
            }
        }

        if (ivTitleRight != null) {
            if (resRight == 0) {
                ivTitleRight.setVisibility(View.GONE);
            } else {
                ivTitleRight.setVisibility(View.VISIBLE);
                ivTitleRight.setImageResource(resRight);
                ivTitleRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onClickTitleRight(v);
                    }
                });
            }
        }

        if (tvTitleLeft != null) {
            if (strLeft == null) {
                tvTitleLeft.setVisibility(View.GONE);
            } else {
                tvTitleLeft.setVisibility(View.VISIBLE);
                tvTitleLeft.setText(strLeft);
                tvTitleLeft.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onClickTitleLeft(v);
                    }
                });
            }
        }
        if (tvTitleContent != null) {
            if (strContent == null) {
                tvTitleContent.setVisibility(View.GONE);
            } else {
                tvTitleContent.setVisibility(View.VISIBLE);
                tvTitleContent.setText(strContent);
            }
        }
        if (tvTitleRight != null) {
            if (strRight == null) {
                tvTitleRight.setVisibility(View.GONE);
            } else {
                tvTitleRight.setVisibility(View.VISIBLE);
                tvTitleRight.setText(strRight);
                tvTitleRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onClickTitleRight(v);
                    }
                });
            }
        }
    }

    protected void onClickTitleLeft(View v) {
        finish();
    }

    protected void onClickTitleRight(View v) {
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginEvent(LoginEvent event) {
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onClickEvent(ClickEvent event) {
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        Glide.with(this).resumeRequests();
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
        Glide.with(this).pauseRequests();
    }

    @Override
    public void finish() {
        BaseActivity.super.finish();
        overridePendingTransition(0, R.anim.slide_close_out_right);
    }

    @Override
    protected void onDestroy() {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        AppManager.getAppManager().finishActivity(this);
    }
}

